//
//  NSString+RichText.m
//  YanTaiBang
//
//  Created by broad zou on 12-10-5.
//  Copyright (c) 2012年 UEXSS. All rights reserved.
//

#import "NSString+RichText.h"
#import "AppUtility.h"

@implementation NSString (RichText)

- (void)parseRichTextToArray:(NSMutableArray*)array_ {
    NSRange range=[self rangeOfString: kRichTextImageBeginFlag];
    NSRange range1=[self rangeOfString: kRichTextImageEndFlag];
    if (range.length>0 && range1.length>0) {
        if (range.location > 0) {
            NSString *str1 = [self substringToIndex:range.location];
            UEXNSLog(@"str1 is %@", str1);
            [array_ addObject:str1];
            NSString *str2 = [self substringWithRange:NSMakeRange(range.location, range1.location+1-range.location)];
            UEXNSLog(@"str2 is %@", str2);
            [array_ addObject:str2];
            NSString *str=[self substringFromIndex:range1.location+1];
            //NSLog(@"str is %@", str);
            [str parseRichTextToArray:array_];
        } else {
            NSString *nextstr=[self substringWithRange:NSMakeRange(range.location, range1.location+1-range.location)];
            if (![nextstr isEqualToString:@""]) {
                [array_ addObject:nextstr];
                NSString *str=[self substringFromIndex:range1.location+1];
                [str parseRichTextToArray:array_];
            } else {
                return;
            }
        }
        
    } else {
        [array_ addObject:self];
    }
}

- (CGSize)sizeOfRichTextWithFontSize:(float)fontSize_ andConstrainedToWidth:(float)width_ andCutNumber:(int)cutNum_{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [self parseRichTextToArray:array];
    UIFont *font = [UIFont systemFontOfSize:fontSize_];  
    CGFloat offX = 0;
    CGFloat offY = 0;
    CGFloat width = 0;
    CGFloat height = fontSize_+4;
	int textNum = 0;
    if (array) {
        for (int i=0;i<array.count;i++) {
            NSString *str=[array objectAtIndex:i];
            if (cutNum_ > 0) {
				textNum += str.length;
				if (textNum > cutNum_) {
					break;
				}
			}
            if ([str hasPrefix:kRichTextImageBeginFlag] && [str hasSuffix:kRichTextImageEndFlag]) {
                if ((offX+(fontSize_+4)) >= width_)
                {
                    offY += (fontSize_+4);
                    offX = 0;
                    width = width_;
                    height = offY + fontSize_+4;
                } 
				offX += (fontSize_+4);
				if (width < width_) {
					width = offX;
				}
            } else {
                for (int j = 0; j < [str length]; j++) {
                    NSString *oneStr = [str substringWithRange:NSMakeRange(j, 1)];
                    CGSize size=[oneStr sizeWithFont:font constrainedToSize:CGSizeMake(width_, 40)];
                    if ((offX+size.width) >= (width_)) {
                        offY += (fontSize_+4);
                        offX = 0;
                        width = width_;
                        height = offY + fontSize_+4;
                    }
					offX += size.width;
					if (width < width_) {
						width = offX;
					}
                }
            }
        }
    }
    [array release];
    CGSize retSize = CGSizeMake(width, height);
    UEXNSLogSize(retSize);
    return retSize;
}

- (void)drawRichTextToView:(UIView*)view_ andFontSize:(float)fontSize_ andConstrainedToWidth:(float)width_ andLeftOff:(float)leftOff_ andTopOff:(float)topOff_ andCutNumber:(int)cutNum_{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [self parseRichTextToArray:array];
    UIFont *font = [UIFont systemFontOfSize:fontSize_];  
    CGFloat offX = 0;
    CGFloat offY = 0;
    CGFloat width = 0;
	int textNum = 0;
	UEXNSLog(@"array is %@", array);
    if (array) {
        for (int i=0;i<array.count;i++) {
            NSString *str=[array objectAtIndex:i];
			if (cutNum_ > 0) {
				textNum += str.length;
				if (textNum > cutNum_) {
					break;
				}
			}
            if ([str hasPrefix:kRichTextImageBeginFlag] && [str hasSuffix:kRichTextImageEndFlag]) {
                if ((offX+(fontSize_+4)) >= width_) {
                    offY += (fontSize_+4);
                    offX = 0;
                    width = width_;
                }
                NSString *imageName=[str substringWithRange:NSMakeRange(1, str.length - 2)];
                UIImageView *ivSmile = [[UIImageView alloc] initWithFrame:CGRectMake(offX+leftOff_, offY+topOff_, fontSize_+4, fontSize_+4)];
                ivSmile.image = [AppUtility getLocalImage:imageName];
                [view_ addSubview:ivSmile];
                [ivSmile release];
				offX += (fontSize_+4);
				if (width < width_) {
					width = offX;
				}
            } else {
                for (int j = 0; j < [str length]; j++) {
                    NSString *oneStr = [str substringWithRange:NSMakeRange(j, 1)];
                    CGSize size=[oneStr sizeWithFont:font constrainedToSize:CGSizeMake(width_, 40)];
                    if ((offX+size.width) >= (width_)) {
                        offY += (fontSize_+4);
                        offX = 0;
                        width = width_;
                    }
                    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(offX+leftOff_,offY+topOff_,size.width,size.height)];
                    label.backgroundColor = [UIColor clearColor];
                    label.font = font;
                    label.text = oneStr;
                    [view_ addSubview:label];
                    [label release];
					offX += size.width;
					if (width < width_) {
						width = offX;
					}
                }
            }
        }
    }
    [array release];
}
- (void)drawRichTextToView:(UIView*)view_ andFontSize:(float)fontSize_ andConstrainedToWidth:(float)width_ andLeftOff:(float)leftOff_ andTopOff:(float)topOff_ wordsColor:(UIColor*)inColor{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [self parseRichTextToArray:array];
    UIFont *font = [UIFont systemFontOfSize:fontSize_];  
    CGFloat offX = 0;
    CGFloat offY = 0;
    CGFloat width = 0;
    if (array) {
        
        for (int i=0;i < [array count] ;i++) {
            NSString *str=[array objectAtIndex:i];
            //UEXNSLog(@"str is %@",str);
            if ([str hasPrefix:kRichTextImageBeginFlag] && [str hasSuffix:kRichTextImageEndFlag]) {
                if ((offX+(fontSize_+4)) >= width_) {
                    offY += (fontSize_+4);
                    offX = 0;
                    width = width_;
                }
                NSString *imageName=[str substringWithRange:NSMakeRange(1, str.length - 2)];
                UIImageView *ivSmile = [[UIImageView alloc] initWithFrame:CGRectMake(offX+leftOff_, offY+topOff_, fontSize_+4, fontSize_+4)];
                ivSmile.image = [AppUtility getLocalImage:imageName];
                [view_ addSubview:ivSmile];
                [ivSmile release];
				offX += (fontSize_+4);
				if (width < width_) {
					width = offX;
				}
            } else {
                for (int j = 0; j < [str length]; j++) {
                    NSString *oneStr = [str substringWithRange:NSMakeRange(j, 1)];
                    CGSize size=[oneStr sizeWithFont:font constrainedToSize:CGSizeMake(width_, 40)];
                    if ((offX+size.width) >= (width_)) {
                        offY += (fontSize_+4);
                        offX = 0;
                        width = width_;
                    }
                    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(offX+leftOff_,offY+topOff_,size.width,size.height)];
                    label.backgroundColor = [UIColor clearColor];
                    label.textColor =inColor;
                    label.font = font;
                    label.text = oneStr;
                    [view_ addSubview:label];
                    [label release];
					offX += size.width;
					if (width < width_) {
						width = offX;
					}
                }
            }
        }
    }
    [array release];
}

@end
